package com.wondersgroup.fjzlpt.tbxt.service.impl.jc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.tbxt.dao.inter.jc.JcJbDao;
import com.wondersgroup.fjzlpt.tbxt.service.inter.jc.JcJbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * 基层季报
 */
@Service
public class JcJbServiceImpl implements JcJbService {

	@Autowired
	private JcJbDao jcJbDao;
	
	@Override
	public JSONObject save(Map map) {
		ResultObject ro = new ResultObject();
		JSONObject jcObj = jcJbDao.queryId(map);
		if(jcObj.isEmpty()){
			UUID uuid = UUID.randomUUID();
			map.put("ID", uuid.toString());
			map.put("ZT","1");
			insert(map,ro);
		}else{
			update(map,ro);
		}
		return (JSONObject)JSON.toJSON(ro);
	}


	@Override
	public JSONObject queryOne(Map<String, String> map) {
		JSONObject nbJson = jcJbDao.queryOne(map);
		if(nbJson.isEmpty()){
			nbJson.put("JGMC",map.get("YLJGMC") );
			nbJson.put("YLJGID",map.get("PTJGID"));
			nbJson.put("JGLB",map.get("WSJGLBDM"));
			nbJson.put("XZQH",map.get("JGXZQHDM"));
			nbJson.put("NF",map.get("NF"));
		}
		return nbJson;
	}
	
	/**
	 * 新增
	 * @param map
	 * @param ro
	 */
	private void insert(Map<String, Object> map, ResultObject ro){
		try {
			Date now = new Date();
			map.put("XZSJ", now);
			map.put("XGSJ", now);
			jcJbDao.insert(map);
		} catch (Exception e) {
			ro.changeFaultState("保存季报失败！");
			throw new RuntimeException("保存基层季报失败！", e);
		}
	}
	
	/**
	 * 更新
	 * @param map
	 * @param ro
	 */
	private void update(Map<String, Object> map, ResultObject ro){
		try {
			map.put("XGSJ", new Date());
			jcJbDao.updateForPrimaryKey(map);
		} catch (Exception e) {
			ro.changeFaultState("更新季报失败！");
			throw new RuntimeException("更新基层季报失败！", e);
		}
	}

}
